/* eslint import/namespace: ['error', { allowComputed: true }] */
import Vue from 'vue'
import Cookies from 'js-cookie'
import SecUI from '@yundi-fe/sec-ui'
import '@yundi-fe/sec-ui/lib/theme-chalk/index.css'
import 'normalize.css/normalize.css'
import '@/styles/index.scss'
import App from './App'
import store from './store'
import router from './router'
import './icons'
import './middle'
import * as filters from './filters'
import pageComponents from '@/components'

// const Vue = window.Vueß
Vue.use(pageComponents)
/**
 * @description dev env default used mock-sercer.
 * if you want to use MockJs, you can execute: mockXHR()
 */
// if (process.env.NODE_ENV === 'development') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }

// element-ui config
Object.entries(SecUI).forEach(([key, component]) => {
  if (/^[A-Z]+/.test(key)) {
    switch (key) {
      case 'Loading':
        Vue.use(component.directive)
        Vue.prototype.$loading = component.service
        break
      case 'Notification':
        Vue.prototype.$notify = component
        break
      case 'Message':
        Vue.prototype.$message = component
        break
      case 'MessageBox':
        Vue.prototype.$msgbox = component
        Vue.prototype.$alert = component.alert
        Vue.prototype.$confirm = component.confirm
        Vue.prototype.$prompt = component.prompt
        break
      default:
        if (component.name) {
          const aName = component.name.split('El')

          if (aName[0]) {
            console.warn('exe replace will handle error', component.name)
          }
          aName.shift()
          aName.unshift('Sec')
          const sName = aName.join('')
          Vue.component(sName, component)
        } else {
          Vue.use(component)
        }
    }
  }
})

Vue.prototype.$ELEMENT = { size: Cookies.get('size') || 'small', zIndex: 3000 }
Vue.directive('noMoreClick', {
  inserted(el, binding) {
    el.addEventListener('click', (e) => {
      el.classList.add('is-disabled')
      el.disabled = true
      setTimeout(() => {
        el.disabled = false
        el.classList.remove('is-disabled')
      }, 2000)
    })
  }
})

Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
new Vue({
  el: '#app',
  router,
  store,
  render: (h) => h(App)
})
